import requests

hosts_list = ['127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4', '::1         localhost localhost.localdomain localhost6 localhost6.localdomain6']
url = 'http://longtan.xxx.cn/controller4ApiService/gethostconfig?environment=qa&name=QAPUB'
req = requests.get(url)
hosts_data = req.text
for i in hosts_data.splitlines():
    hosts_list.append(i)
with open('/etc/hosts', 'w', encoding='utf-8') as file:
    for i in hosts_list:
        file.write(i + '\n')
print('success!')